package domain.terrain;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import components.scenes.CollisionablesScene;


public class Parser {

	
	public void pixelsToScene(MatrixComponent terrain, BufferedImage image){

	}
	
	
	private boolean isSolid(int rgb) {
		Color color = new Color(rgb);
		
		if (color.getRed() < 130){
			return true;
		}else{
			return false;
		}
		
	}


	public BufferedImage getImage(String path){
		BufferedImage image = null;

		try {
			image = ImageIO.read(Parser.class.getResource(path));
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return image;
	}
	public static BufferedImage image(String path){
		BufferedImage image = null;

		try {
			image = ImageIO.read(Parser.class.getResource(path));
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return image;
	}
	
	public static BufferedImage applyMask(BufferedImage image, BufferedImage mask){
		BufferedImage finalImage = new BufferedImage(mask.getWidth(), mask.getWidth(), BufferedImage.TYPE_INT_ARGB);
		for (int y = 0; y < image.getHeight(); y++) {
		    for (int x = 0; x < image.getWidth(); x++) {
		        Color c = new Color(image.getRGB(x, y));
		        Color maskC = new Color(mask.getRGB(x, y));
		        Color maskedColor;
		        if (maskC.getRed() > 0){
		        	maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(),
			                0);
		        }
		        else{
			        maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(),
			                255);
		        }

		        finalImage.setRGB(x, y, maskedColor.getRGB());
		    }
		}
		return finalImage;
	}


	public static BufferedImage scaledBufferedImageFrom(int width, int height, String path) {
		Image texture = Parser.image(path).getScaledInstance(1000, 1000, BufferedImage.SCALE_SMOOTH);
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		Graphics g = image.createGraphics();

	    // Paint the image onto the buffered image
	    g.drawImage(texture, 0, 0, null);
	    g.dispose();
	    
	    return image;
	}


	public static BufferedImage applyMaskOn(BufferedImage image,
			BufferedImage mask, int fromx, int fromy, int width, int height) {

		int sx, sy, ex, ey;
		
		if(fromx < 0){
			sx = 0;
		} else sx = fromx;
		
		if (fromy < 0){
			sy = 0;
		} else sy = fromy;
		
		ex = fromx + width;
		ey = fromy + height;
		
		if (ex > mask.getWidth()){
			ex = mask.getWidth();
		}
		if (ey > mask.getHeight()){
			ey = mask.getHeight();
		}

		
		for (int y = sy; y < ey; y++) {
		    for (int x = sx ; x < ex; x++) {
		        Color c = new Color(image.getRGB(x, y));
		        Color maskC = new Color(mask.getRGB(x, y));
		        Color maskedColor;
		        if (maskC.getRed() > 0){
		        	maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(),
			                0);
		        }
		        else{
			        maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(),
			                255);
		        }

		        image.setRGB(x, y, maskedColor.getRGB());
		    }
		}
		return image;
	}

	
}
